﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class TerisController : MonoBehaviour
{
    public Teris[] TerisTemplates;
    public Teris Boss1Template;
    public GridManager GridManager;
    public Player Player;
    public GameObject EmptySprite, Enemy1Sprite, Enemy2Sprite, Boss1Sprite, BoxSprite, TreasureSprite;
    public GameObject LTPoint;
    public Bag Bag;

    public AudioSource DropAudio;

    private TerisInstance NowTeris, Boss1Instance;
    private int leftColumn;
    private GameObject[] previewSprite;
    private int boss1Appeared = 0;
    // Start is called before the first frame update
    void Start()
    {
        Boss1Instance = new TerisInstance(Boss1Template);
    }

    // Update is called once per frame
    private int MoveFlag = 0;
    void Update()
    {
        if(MoveFlag == 0)
            return;
        if(Input.GetKeyDown(KeyCode.Q)) {
            ClearDisplay();
            Player.SetMoveable();
            MoveFlag = 0;
            return;
        }
        else if(Input.GetKeyDown(KeyCode.W)) {
            if(NowTeris.Teris.SquareCount == 36)
                return;
            NowTeris.Teris = NowTeris.Teris.Next;
            if(leftColumn + NowTeris.Teris.Width > GridManager.Width) {
                leftColumn = GridManager.Width - NowTeris.Teris.Width;
            }
            RefreshDisplay();
        }
        else if(Input.GetKeyDown(KeyCode.A)) {
            if(leftColumn > 0) {
                leftColumn--;
                UpdateDisplay();
            }
        }
        else if(Input.GetKeyDown(KeyCode.D)) {
            if(leftColumn + NowTeris.Teris.Width < GridManager.Width) {
                leftColumn++;
                UpdateDisplay();
            }
        }
        else if(Input.GetKeyDown(KeyCode.Keypad0) || Input.GetKeyDown(KeyCode.Alpha0)) {
            if(Player.RoundsLeft != 0) {
                return;
            }
            if(boss1Appeared == 1) {
                return;
            }
            StartSetTeris(9);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Alpha1)) {
            StartSetTeris(0);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) {
            StartSetTeris(1);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) {
            StartSetTeris(2);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) {
            StartSetTeris(3);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) {
            StartSetTeris(4);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6)) {
            StartSetTeris(5);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7)) {
            StartSetTeris(6);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8)) {
            StartSetTeris(7);
        }
        else if(Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9)) {
            StartSetTeris(8);
        }
        else if(Input.GetKeyDown(KeyCode.Return)) {
            int LDPointY = GridManager.GetFallPos(NowTeris, leftColumn);
            DropAudio.Play();
            StartCoroutine(StartEliminateCoroutine(LDPointY));
            if(NowTeris.Teris.SquareCount == 36) {
                boss1Appeared = 1;
                Player.Boss1Down();
            }
            else {
                Bag.DestroyCurrent();
                Player.DecreaseRound();
            }
            MoveFlag = 0;
        }
    }

    IEnumerator StartEliminateCoroutine(int LDPointY) {
        // Falling...
        foreach(GameObject previewO in previewSprite) {
            previewO.GetComponent<SpriteRenderer>().color = Color.white;
        }
        int count = GridManager.Height - LDPointY - NowTeris.Teris.MaxHeight;
        StartCoroutine(FallingCoroutine(count, 0.3f / count));
        yield return new WaitForSeconds(0.3f);
        ClearDisplay();
        GridManager.PushTeris(NowTeris, leftColumn, LDPointY);
        yield return new WaitForSeconds(0.3f);
        GridManager.EliminateTeris();
    }

    IEnumerator FallingCoroutine(int count, float duration) {
        if(count == 0) {
            yield break;
        }
        yield return new WaitForSeconds(duration);
        LTPoint.transform.position -= new Vector3(0, 1, 0) * GridManager.SquareSize;
        StartCoroutine(FallingCoroutine(count - 1, duration));
    }

    private void UpdateDisplay() {
        LTPoint.transform.position = GridManager.transform.position + new Vector3(0, GridManager.Height - 1, 0) * GridManager.SquareSize;
        LTPoint.transform.position += new Vector3(leftColumn, 0, 0) * GridManager.SquareSize;
    }

    private void RefreshDisplay() {
        for(int i = 0; i < NowTeris.Teris.SquareCount; i++) {
            previewSprite[i].transform.position = LTPoint.transform.position
                + new Vector3(NowTeris.Teris.PosX[i] - 1, NowTeris.Teris.PosY[i] - NowTeris.Teris.MaxHeight, 0) * GridManager.SquareSize;
        }
        UpdateDisplay();
    }

    private void InstantiateDisplay() {
        LTPoint.SetActive(true);
        previewSprite = new GameObject[NowTeris.Teris.SquareCount];
        for(int i = 0; i < NowTeris.Teris.SquareCount; i++) {
            if(NowTeris.Contents[i] == Slot.State.none) {
                previewSprite[i] = Instantiate(EmptySprite, LTPoint.transform);
                previewSprite[i].GetComponent<SpriteRenderer>().sprite = null;
            }
            if(NowTeris.Contents[i] == Slot.State.empty) {
                previewSprite[i] = Instantiate(EmptySprite, LTPoint.transform);
            }
            else if(NowTeris.Contents[i] == Slot.State.enemy) {
                previewSprite[i] = Instantiate(Enemy1Sprite, LTPoint.transform);
            }
            else if(NowTeris.Contents[i] == Slot.State.enemy2) {
                previewSprite[i] = Instantiate(Enemy2Sprite, LTPoint.transform);
            }
            else if(NowTeris.Contents[i] == Slot.State.boss1) {
                previewSprite[i] = Instantiate(Boss1Sprite, LTPoint.transform);
            }
            else if(NowTeris.Contents[i] == Slot.State.box) {
                previewSprite[i] = Instantiate(BoxSprite, LTPoint.transform);
            }
            else if(NowTeris.Contents[i] == Slot.State.treasure) {
                previewSprite[i] = Instantiate(TreasureSprite, LTPoint.transform);
            }
            previewSprite[i].SetActive(true);
        }
    }

    private void ClearDisplay(int option = 1) {
        if(previewSprite == null)
            return;
        foreach(GameObject previewO in previewSprite) {
            DestroyImmediate(previewO);
        }
        previewSprite = null;
        LTPoint.SetActive(false);
    }

    public void StartSetTeris(int terisNum = -1) {
        ResetTeris();
        // /*choose a teris...
        if(terisNum == -1) {
            terisNum = Bag.GetFirstTeirsNum();
            if(terisNum == -1) {
                Debug.Log("No Item!");
                Player.SetMoveable();
                MoveFlag = 0;
                return;
            }
        }
        else if(terisNum != 9){
            if(!Bag.Exist(terisNum)) {
                //...
                return;
            }
        }
        if(terisNum == 9) {
            NowTeris = Boss1Instance;
        }
        else {
            NowTeris = Bag.GetTeris(terisNum);
        }
        
        //...*/

        ClearDisplay();
        InstantiateDisplay();
        leftColumn = 0;
        RefreshDisplay();
        MoveFlag = 1;
    }

    public void AddTeris(int option) {
        //choss teris shape...
        int templateNum = -1;
        int t = Random.Range(0, 3);
        if(t == 0) { // 6块
            templateNum = Random.Range(9, 16);
        }
        else {  // 4块
            templateNum = Random.Range(1, 8);
        }
        t = Random.Range(0, 18);
        if(t == 0) { //2 块
            templateNum = 0;
        }
        t = Random.Range(0, 25);
        if(t == 0) { //5 块
            templateNum = 8;
        }
        //...*/
        //templateNum = 3;
        NowTeris = new TerisInstance(TerisTemplates[templateNum], option);

        InstantiateDisplay();
        RefreshDisplay();
        
        foreach(GameObject previewO in previewSprite) {
            previewO.GetComponent<SpriteRenderer>().color = Color.white;
        }
        Bag.AddTeris(NowTeris, LTPoint);
        ClearDisplay(option);
    }

    public void ResetTeris() {
        NowTeris = null;
        ;//...
    }
}
